/*
 * Author: Alvin
 * Modified By: Alvin
 * Created Date: 2023-10-08 15:28:25
 * Last Modified: 2024-04-24 15:44:28
 * Description:
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/list_empty_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/text_width.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_device_list/site_device_list_controller.dart';
import 'package:yt_devops/pages/site/site_device_list/site_device_list_device.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import '../../../components/tips.dart';

class SiteDeviceListView extends StatelessWidget {
  SiteDeviceListView({Key? key}) : super(key: key);
  final controller = Get.put(SiteDeviceListController());
  final state = Get.find<SiteDeviceListController>().state;
  @override
  Widget build(BuildContext context) {
    return GetBuilder<SiteDeviceListController>(
      builder: (_) => Scaffold(
        backgroundColor: Colour.backgroundColor,
        appBar: AppBarWidget.withRightActions(
          ResKey.deviceManage.tr,
          actions: [
            if (!controller.isSort&& controller.state.dataList.length >= 2 && controller.sortPermission)
              IconButton(
                icon: Image.asset('assets/images/device_sort.png'),
                iconSize: 10,
                onPressed: controller.sortDevice,
              ),
            if (!controller.isSort && controller.addPermission)
              IconButton(
                icon: const Icon(Icons.add),
                iconSize: 30,
                onPressed: controller.addNewDevice,
              ),
            if (controller.isSort)
              TextButton(
                  onPressed: () {
                    controller.sortDeviceCompete();
                  },
                  child: Text(
                    ResKey.finished.tr,
                    style: ytTextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: Colour.primaryColor),
                  )),
          ],
        ),
        body: SafeArea(
          child: Stack(
            children: [
              SizedBox(
                height: ScreenUtil().screenHeight - ScreenUtil().statusBarHeight,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    controller.state.dataList.isEmpty
                        ? ListEmptyWidget(topPosition: 82)
                        : Expanded(
                            child: SmartRefresher(
                                enablePullDown: true,
                                enablePullUp: false,
                                controller: controller.state.refreshController,
                                onRefresh: controller.refreshData,
                                // onLoading: controller.onLoading,
                                header: ListTips.classicHeader,
                                footer: ListTips.generateClassicFooter,
                                child: controller.state.dataList.isEmpty
                                    ? Container()
                                    :
                                    // controller.isSort?
                                    ReorderableListView.builder(
                                        buildDefaultDragHandles: controller.isSort,
                                        scrollController: controller.scrollController,
                                        itemCount: state.dataList.length,
                                        itemBuilder: (context, index) => SiteDeviceListDeviceWidget(
                                          key: ValueKey(controller.state.dataList[index].deviceId),
                                          controller.state.dataList[index],
                                          controller.isSort,
                                          onItemTaped: controller.tapDeviceHandler,
                                          sortToTopClick: (model) {
                                            final item = controller.state.dataList.removeAt(index);
                                            controller.state.dataList.insert(0, item);
                                            controller.update();
                                          },
                                          unbindingPermission: controller.unbindingPermission,
                                        ),
                                        onReorder: (int oldIndex, int newIndex) {
                                          if (oldIndex < newIndex) {
                                            newIndex -= 1;
                                          }
                                          final item = controller.state.dataList.removeAt(oldIndex);
                                          controller.state.dataList.insert(newIndex, item);
                                          controller.update();
                                        },
                                      )
                                //     :ListView.builder(
                                //   itemCount: state.dataList.length,
                                //   itemBuilder: (context, index) =>
                                //       SiteDeviceListDeviceWidget(
                                //         controller.state.dataList[index],
                                //         controller.isSort,
                                //         onItemTaped: controller.tapDeviceHandler,
                                //         sortToTopClick: (model){
                                //           final item = controller.state.dataList.removeAt(index);
                                //           controller.state.dataList.insert(0, item);
                                //           controller.update();
                                //         },
                                //       ),
                                // )
                                ),
                          )
                  ],
                ),
              ),
              _popWidget()
            ],
          ),
        ),
      ),
    );
  }

  _popWidget() {
    double width = 149.w;
    var value1 = ResKey.siteAccessDeviceManualEntry.tr;
    double width1 = TextWidth.calculateTextSize(value1, ytTextStyle(fontSize: 16));
    var value2 = ResKey.siteAccessDeviceManualEntry.tr;
    double width2 = TextWidth.calculateTextSize(value2, ytTextStyle(fontSize: 16));
    if (width1 > width2) {
      width = width1;
    } else {
      width = width2;
    }
    Log.d('width===' + ScreenUtil().statusBarHeight.toString());
    Log.d('width===' + ScreenUtil().bottomBarHeight.toString());
    return Positioned(
      // height: ,
        child: Visibility(
      visible: controller.isShowPop,
      // visible:false,
      child: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: controller.addNewDevice,
          child: Container(
            height: ScreenUtil().screenHeight,
            width: ScreenUtil().screenWidth,
            // color: Colors.green,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Container(
                  child: Container(
                    height: 116 + 15,
                    width: width + 66.w  + 10,
                    margin: EdgeInsets.only(left:5,right: 5),
                    padding: EdgeInsets.all(5),
                    //设置背景图片
                    decoration:  BoxDecoration(
                        boxShadow: [
                          BoxShadow(
                              color: Colors.black12.withOpacity(0.01),
                              offset: Offset(-2.0, 0.0), //阴影xy轴偏移量
                              blurRadius: 1, //阴影模糊程度
                              spreadRadius: 1//阴影扩散程度
                          )
                        ],               // color: Colors.red,
                      borderRadius: const BorderRadius.all(Radius.circular(10)),
                      image: const DecorationImage(
                        alignment: Alignment.centerRight,
                        image: AssetImage(ImageAssets.siteAccessBg), //298/232
                        fit: BoxFit.cover,
                      ),
                    ),

                    // alignment: Alignment.center,
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Gaps.vGap10,
                          GestureDetector(
                              behavior: HitTestBehavior.opaque,
                              child: Container(
                                // color: Colors.red,
                                height: 55,
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    // Gaps.hGap10,
                                    Container(
                                      margin: const EdgeInsets.only(left: 21, top: 5),
                                      // color: Colors.red,
                                      child: Image.asset(ImageAssets.siteAccessManualEntry),
                                    ),
                                    Container(
                                      margin: const EdgeInsets.only(left: 5, top: 8),
                                      child: Text(
                                        ResKey.siteAccessDeviceManualEntry.tr,
                                        style: ytTextStyle(fontSize: 16),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              onTap: () {
                                controller.manualEntryOrScanEntry(0);
                              }),
                          Divider(
                            color: Colour.dividerColor,
                            indent: 21.w,
                            endIndent: 21.w,
                            height: 1,
                          ),
                          // Gaps.vGap10,
                          GestureDetector(
                              behavior: HitTestBehavior.opaque,
                              child: Container(
                                // color: Colors.grey,
                                height: 55,
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    // Gaps.hGap10,
                                    Container(
                                      margin: const EdgeInsets.only(left: 21),
                                      child: Image.asset(ImageAssets.siteAccessScanEntry),
                                    ),
                                    Container(
                                      margin: const EdgeInsets.only(left: 5, top: 2),
                                      child: Text(
                                        ResKey.siteAccessDeviceScannerEntry.tr,
                                        style: ytTextStyle(fontSize: 16),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              onTap: () {
                                controller.manualEntryOrScanEntry(1);
                              }),
                        ]),
                  ),
                )
              ],
            ),
          )),
    ));
  }
}
